<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="isShow"
    width="500px"
    @closed="reset"
  >
    <el-form
      ref="dataForm"
      class="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="100px"
    >
      <el-form-item label="检查类型:" prop="itemName">
        <el-input v-model.trim="form.itemName" :maxlength="100" placeholder="请输入安检项目名称" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset"> 关闭 </el-button>
      <el-button :loading="$loadingPlugin.resetSubmit" type="primary" @click="save">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { fillingcheckTypeSave, fillingcheckTypeUpdate } from '@/api'
  export default {
    data() {
      return {
        isShow: false,
        title: '',
        form: {
          itemName: ''
        },
        rules: {
          itemName: [{ required: true, message: '请输入检查类型', trigger: 'change' }]
        },
        rowData: {},
        type: 1
      }
    },
    methods: {
      show(row, type, title) {
        this.rowData = row
        this.title = type === 1 ? `新增${row.label}` : `编辑${title}`
        this.type = type
        this.isShow = true
        if (type === 2) this.form.itemName = row.itemName
      },
      reset() {
        this.$refs['dataForm'].resetFields()
        this.isShow = false
        this.title = '新增'
        this.form.itemName = ''
        this.type = 1
      },
      async addSubmit() {
        const params = {
          itemId: this.rowData.itemId,
          itemType: this.rowData.itemType,
          itemName: this.form.itemName
        }
        const { status } = await fillingcheckTypeSave(params)
        if (status === 200) {
          this.$notify({
            title: '提示',
            message: `新增成功`,
            type: 'success',
            duration: 2000
          })
          this.$emit('refresh')
          this.reset()
        }
      },
      async editSubmit() {
        const params = {
          itemId: this.rowData.itemId,
          itemType: this.rowData.itemType,
          itemName: this.form.itemName
        }
        const { status } = await fillingcheckTypeUpdate(params)
        if (status === 200) {
          this.$notify({
            title: '提示',
            message: `修改成功`,
            type: 'success',
            duration: 2000
          })
          this.$emit('refresh')
          this.reset()
        }
      },
      save() {
        this.$refs.dataForm.validate((valid) => {
          if (!valid) return
          if (this.type === 1) this.addSubmit()
          else this.editSubmit()
        })
      }
    }
  }
</script>

<style lang="scss" scoped></style>
